package com.sikaryofficial.workbench.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sikaryofficial.common.core.utils.SpringUtils;
import com.sikaryofficial.common.redis.service.RedisService;
import com.sikaryofficial.workbench.constant.BusinessTypeEnum;
import com.sikaryofficial.workbench.constant.OrderConstant;
import com.sikaryofficial.workbench.constant.RedisCacheKey;
import com.sikaryofficial.workbench.domain.entity.BusinessCode;
import com.sikaryofficial.workbench.service.ICodeService;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.util.Objects;

/**
 * @author : wuweihong
 * @desc : 生成实体编码的工具类
 * @date : 2024-10-09
 */

public class EntityCodeUtils {

    private EntityCodeUtils() {}

    private static String generateNo(BusinessTypeEnum businessType, String prefix) {
        RedisService redisService = SpringUtils.getBean(RedisService.class);
        ICodeService codeService = SpringUtils.getBean(ICodeService.class);
        String cacheKey = MessageFormat.format(RedisCacheKey.BUSINESS_INCR_NO, businessType.getCode(), prefix);
        Integer currValue;
        BusinessCode code = codeService.getOne(new LambdaQueryWrapper<BusinessCode>()
                .eq(BusinessCode::getBusinessType, BusinessTypeEnum.ORDER_HEADER.getCode())
                .eq(BusinessCode::getPrefix, prefix)
        );
        if (Boolean.TRUE.equals(redisService.hasKey(cacheKey))) {
            currValue = Integer.parseInt(redisService.increaseNum(cacheKey).toString());
        }else {
            if (Objects.isNull(code)) {
                code = new BusinessCode();
                code.setBusinessCodeId(IdWorker.getId());
                code.setPrefix(prefix);
                code.setBusinessType(businessType.getCode());
                currValue = 1;
            }else {
                currValue = code.getCurrValue()+1;
            }
            code.setCurrValue(currValue);
            redisService.setCacheObject(cacheKey, currValue);
        }
        codeService.saveOrUpdate(code);
        DecimalFormat df = new DecimalFormat(OrderConstant.ORDER_NO_FORMAT);
        return prefix + df.format(currValue);
    }

    public static String generateOrderHeaderNo() {
        BusinessTypeEnum businessType = BusinessTypeEnum.ORDER_HEADER;
        LocalDate localDate = LocalDate.now();
        int currYear = localDate.getYear();
        int currMonth = localDate.getMonthValue();
        DecimalFormat monthDf = new DecimalFormat(OrderConstant.MONTH_FORMAT);
        String prefix = currYear + monthDf.format(currMonth);
        return generateNo(businessType, prefix);
    }

    public static String generateOrderItemNo(String brandNo) {
        BusinessTypeEnum businessType = BusinessTypeEnum.ORDER_ITEM;
        LocalDate localDate = LocalDate.now();
        int currYear = localDate.getYear();
        int currMonth = localDate.getMonthValue();
        DecimalFormat monthDf = new DecimalFormat(OrderConstant.MONTH_FORMAT);
        String prefix = brandNo + currYear + monthDf.format(currMonth);
        return generateNo(businessType, prefix);
    }

}
